package institution_service

import (
	"github.com/EDDYCJY/go-gin-example/models"
)

type GetSample struct {
	InstitutionID int
	PageNum       int
	PageSize      int
}

func (t *GetSample) GetSample() (interface{}, error) {
	maps := map[string]interface{}{
		"institution_id": t.InstitutionID,
	}
	total, _ := models.GetSampleTotal(maps)
	goodsList, _ := models.GetSamplePage([]string{"sample_id", "user_id", "create_time", "goods_id", "goods_num", "streamer_id", "express_name", "express_no"}, maps, t.PageNum, t.PageSize, "create_time desc")
	var lists []interface{}
	for _, v := range goodsList {
		params := map[string]interface{}{
			"sample_id":       v.SampleId,
			"express_name":    v.ExpressName,
			"express_no":      v.ExpressNo,
			"goods_id":        v.Goods.GoodsID,
			"goods_name":      v.Goods.GoodsName,
			"goods_spec":      v.Goods.GoodsSpec,
			"goods_no":        v.Goods.GoodsNo,
			"create_time":     v.CreateTime.Format("2006-01-02 15:04:05"),
			"goods_img":       v.Goods.GoodsImg,
			"goods_num":       v.GoodsNum,
			"streamer_name":   v.Streamer.StreamerName,
			"streamer_mobile": v.Streamer.StreamerMobile,
			"user_name":       v.User.UserName,
		}
		lists = append(lists, params)
	}
	return map[string]interface{}{
		"lists": lists,
		"total": total,
	}, nil
}

// Login 定义登录请求结构体
type AddSample struct {
	InstitutionID int
	GoodsID       int
	GoodsNum      int
	ExpressNo     string
	ExpressName   string
	StreamerID    int
	UserID        int
}

// AddSample 执行用户登录逻辑
func (t *AddSample) AddSample() (map[string]interface{}, error) {
	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"streamer_id":    t.StreamerID,
		"express_no":     t.ExpressNo,
		"express_name":   t.ExpressName,
		"goods_id":       t.GoodsID,
		"goods_num":      t.GoodsNum,
		"user_id":        t.UserID,
	}
	_, err := models.AddSample(maps)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

// Login 定义登录请求结构体
type EditSample struct {
	SampleID      int    `form:"id" valid:"Required;"`
	ExpressNo     string `alias:"快递单号" form:"express_no"  json:"express_no" `
	ExpressName   string `alias:"快递名称" form:"express_name"  json:"express_name" `
	InstitutionID int
}

// EditSample 执行用户登录逻辑
func (t *EditSample) EditSample() (map[string]interface{}, error) {

	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"sample_id":      t.SampleID,
	}
	var update = map[string]interface{}{
		"express_no":   t.ExpressNo,
		"express_name": t.ExpressName,
	}
	err := models.EditSample(maps, update)
	if err != nil {
		return nil, err
	}
	return nil, nil
}
